package com.dangdi.util;

import javax.validation.constraints.Null;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.CharBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SocketServer2 {

    private ServerSocket serverSocket;
    private Socket socket;
    private int port = 5055;
    List<Map<String, Object>> clients = new ArrayList<Map<String, Object>>();

    public List<Map<String, Object>> getClients() {
        return clients;
    }

    public void setClients(List<Map<String, Object>> clients) {
        this.clients = clients;
    }

    private void start() throws Exception {
        serverSocket = new ServerSocket(port);
        while (true) {
            socket = serverSocket.accept();
            Map<String, Object> s = new HashMap<String, Object>();
            s.put("socket", socket);
            s.put("ip", socket.getInetAddress().toString());
            clients.add(s);
            new Thread(new SocketThread(socket)).start();// 多线程阻塞
        }
    }

    /**
     * 处理socket连接s
     */
    class SocketThread implements Runnable {
        private Socket socket;
        private String temp = "";

        public SocketThread(Socket socket) {
            this.socket = socket;
        }

        @SuppressWarnings("unused")
        public void run() {
            try {
                InputStream reader = socket.getInputStream();
                OutputStream writer = socket.getOutputStream();
                byte line[] = new byte[2048];      //回复数据长度
                while (reader.read(line) != -1) {
                    temp = new String(line).trim();
                    System.out.println(temp);
//                    System.out.println(temp.length());
//                    writer.write( SocketMessageController.controller(temp).getBytes());
                    PrintWriter pw = new PrintWriter(new OutputStreamWriter(writer));
//                    pw.write("good");
                    line = new byte[2048];
                    pw.flush();
                }
            } catch (IOException e) {
//                e.printStackTrace();
                System.out.println("客户端挂了");
            } finally {

                if (socket != null) {
                    if (!socket.isClosed()) {
                        try {
                            socket.close();
                            for (int i = 0; i < clients.size(); i++) {
                                Socket s = (Socket) clients.get(i).get("socket");
                                if (socket.getInetAddress().toString().equals(s.getInetAddress())) {
                                    clients.remove(i);
                                    System.out.println("客户端离开了: ip=" + s.getInetAddress() + " 客户端标识=" + clients.get(i).get("terminalSymbol").toString());
                                }
                            }
                        } catch (IOException e) {
                            System.out.println("不知道怎么了");
                        }
                    } else {
                        System.out.println("客户端下线了");
                    }
                } else {
                    System.out.println("客户端早已断了");
                }
//                System.out.println("不知道干什么的");

            }
        }

    }

    public static void main(String[] args) {
        SocketServer2 s = new SocketServer2();
        try {
            s.start();
        } catch (Exception e) {
            System.out.println(e);
//            e.printStackTrace();
        }
    }

}